#
#  Copyright 2022 The Open Islands Authors. All Rights Reserved.
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.
#
from pyoi.constant import FederatedMode
from .operator_input import OperatorInput
from .operator_output import OperatorOutput
from pyoi.types import BaseType


class RuntimeConfig(BaseType):
    def __init__(self, **kwargs):
        self.jobType = ""
        self.name: str = None
        self.module: str = None
        self.entryPoint: str = None
        self.role: str = None
        self.nodeId: str = None
        self.nodes: dict = None
        self.input: OperatorInput = None
        self.output: OperatorOutput = None

        self.numPartitions: int = None
        self.numFederatedSenders: int = None
        self.federatedMode: FederatedMode = None

        self.serviceAddresses: dict = {}
        self.serviceParams: dict = {}
        self.operatorParams: dict = {}

        for k, v in kwargs.items():
            if hasattr(self, k):
                setattr(self, k, v)
        self.parsing_string_attr()

    def parsing_string_attr(self):
        if self.numPartitions is not None:
            self.numPartitions = int(self.numPartitions)
        if self.numFederatedSenders is not None:
            self.numFederatedSenders = int(self.numFederatedSenders)
        if self.federatedMode is not None:
            self.federatedMode = FederatedMode[self.federatedMode]
        if self.input is not None:
            self.input = OperatorInput.from_json(self.input)
        if self.output is not None:
            self.output = OperatorOutput.from_json(self.output)

    def to_dict(self):
        d = {}
        for k, v in self.__dict__.items():
            if v is None:
                continue
            d[k] = v
        return d
